//数据库-存入商品订单
const cloud = require('wx-server-sdk')
const db = cloud.database()
const _ = db.command

exports.main = async (event, context) => {
  var no = Math.floor(Math.random() * 999999);
  var date = new Date();
  var key = date.getTime() + "" + no;//暂时这样写。
  try {
     //删除覆盖
     if(event.type2 == "set"){
      return await db.collection('userInfo').where({
        openid: event.openid
      })
      .update({
        data: {
          orderShops: _.set(event.list)
        },
      })
    }
    //插入
    return await db.collection('userInfo').where({
      openid: event.openid
    })
    .update({
      data: {
        orderShops: _.push({
          key: key,
          shopList:event.shopList,
          dateTime:new Date().getTime(),
          status:event.status,
          address:event.address,
          totalNum:event.totalNum,
          totalPrice:event.totalPrice
        })
      },
    })
  } catch(e) {
    console.error("database orderShops error!-->")
    console.error(e)
  }
}